package threecreateways;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

// 多线程同步下载图片方式二
public class  PictureDownDemo2 implements Runnable{
    private String url;
    private String name;

    public PictureDownDemo2(String url, String name) {
        this.url = url;
        this.name = name;
    }

    public void run() {
        WebDownloader1 webDownloader = new WebDownloader1();
        webDownloader.downloader(url,name);
        System.out.println("下载了网络文件："+name);
    }

    public static void main(String[] args) {
        PictureDownDemo2 thread1 = new PictureDownDemo2("https://tse1-mm.cn.bing.net/th/id/OIP-C.7KW5GT7NQ8yUGlBbCHEm0gHaNK?pid=ImgDet&rs=1", "文件1");
        PictureDownDemo2 thread2 = new PictureDownDemo2("https://ts1.cn.mm.bing.net/th/id/R-C.df4462fabf18edd07195679a5f8a37e5?rik=FnNvr9jWWjHCVQ&riu=http%3a%2f%2fseopic.699pic.com%2fphoto%2f50059%2f8720.jpg_wh1200.jpg&ehk=ofb4q76uCls2S07aIlc8%2bab3H5zwrmj%2bhqiZ%2fyw3Ghw%3d&risl=&pid=ImgRaw&r=0", "文件2");
        PictureDownDemo2 thread3 = new PictureDownDemo2("https://desk-fd.zol-img.com.cn/t_s960x600c5/g5/M00/0A/0B/ChMkJl4X3nmIaIfnAAZRyAgdiAcAAwQAwBW-9EABlHg809.jpg", "文件3");

        new Thread(thread1).start();
        new Thread(thread2).start();
        new Thread(thread3).start();
    }
}

// 下载器    https://commons.apache.org/proper/commons-io/download_io.cgi
class WebDownloader1{
    public void downloader(String url, String name){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
